//
//  RegisterViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "RegisterViewController.h"
#import <Masonry.h>
#import <SVProgressHUD.h>

NSInteger changedSecond = 10;

@interface RegisterViewController ()

@property (weak,nonatomic)UITextField *accountText;

@property (weak,nonatomic)UITextField *passwordText;

@property (weak,nonatomic)UIButton *testButton;

@property (weak,nonatomic)UIButton *chooseButton;

@property (weak,nonatomic)UILabel *readLabel;

@property (weak,nonatomic)UILabel *protocolLabel;

@property (weak,nonatomic)UIButton *registerButton;

@property (strong,nonatomic)NSTimer *timer;



@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"注册";
    
    [self creatRegisterView];
    
}
/*
 Assigning retained object to weak variable; object will be released after assignment,代码创建控件进行布局时,声明属性如果用weak修饰在懒加载中_passwordText = [[UITextField alloc]init];会出现警告,解决:1.用strong修饰 2.UITextField *text = [[UITextField alloc]init];_accountText = text;,让其有个强引用
 */
- (UITextField *)accountText{
    
    if(!_accountText){
        
        UITextField *text = [[UITextField alloc]init];
        
        _accountText = text;
        
        [self.view addSubview:_accountText];
    }
    return _accountText;
}

- (UITextField *)passwordText{
    
    if(!_passwordText){
        
        UITextField *text = [[UITextField alloc]init];
        
        _passwordText = text;
        
        [self.view addSubview:_passwordText];
    }
    
    return _passwordText;
}

- (UIButton *)testButton{
    
    if(!_testButton){
        
        UIButton *button = [[UIButton alloc]init];
        
        _testButton = button;
        
        [self.view addSubview:_testButton];
    }
    return _testButton;
}

- (UIButton *)chooseButton{
    
    if(!_chooseButton){
        
        UIButton *button = [[UIButton alloc]init];
        
        _chooseButton = button;
        
        [self.view addSubview:_chooseButton];
    }
    return _chooseButton;
}

- (UILabel *)readLabel{

    if(!_readLabel){
    
        UILabel *labael = [[UILabel alloc]init];
        
        _readLabel = labael;
        
        [self.view addSubview:_readLabel];
    }
    return _readLabel;
}

- (UILabel *)protocolLabel{
    
    if(!_protocolLabel){
        
        UILabel *labael = [[UILabel alloc]init];
        
        _protocolLabel = labael;
        
        [self.view addSubview:_protocolLabel];
    }
    return _protocolLabel;
}

- (UIButton *)registerButton{

    if(!_registerButton){
        
        UIButton *button = [[UIButton alloc]init];
        
        _registerButton = button;
        
        [self.view addSubview:_registerButton];
    }
    return _registerButton;
    
}

#pragma mark -- 注册界面的控件布局

- (void)creatRegisterView{
    /**
     *  手机号
     */
    self.accountText.placeholder = @"请输入手机号码:";
    
    self.accountText.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    
    [self.accountText setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    self.accountText.borderStyle = UITextBorderStyleRoundedRect;
    
    self.accountText.clearButtonMode = UITextFieldViewModeAlways;
    
    [self.accountText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).offset(100);
        make.leading.equalTo(self.view).offset(30);
        make.trailing.equalTo(self.view).offset(-30);
    }];
    
    /**
     *  输入验证码
     */
    
    self.passwordText.placeholder = @"请输入验证码:";
    
    [self.passwordText setAutocorrectionType:UITextAutocorrectionTypeNo];
    
    self.passwordText.borderStyle = UITextBorderStyleRoundedRect;
    
    self.passwordText.clearButtonMode = UITextFieldViewModeAlways;
    
    self.passwordText.secureTextEntry = YES;
    
    [self.passwordText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.view).multipliedBy(0.6);
        make.leading.equalTo(self.accountText);
        make.width.equalTo(@130);
    }];
    
    /**
     *  验证码
     */
    [self.testButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    
    [self.testButton setBackgroundColor:[UIColor grayColor]];
    
    [self.testButton setTintColor:[UIColor whiteColor]];
    
    [self.testButton addTarget:self action:@selector(clickTestButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.testButton.layer.cornerRadius = 5;
    
    self.testButton.clipsToBounds = YES;
    
    self.testButton.titleLabel.font = [UIFont systemFontOfSize:15];
    
    [self.testButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(self.passwordText);
        make.trailing.equalTo(self.accountText);
        make.width.equalTo(@160);
    }];
    

    
    /**
     *  勾选按钮
     */
    [self.chooseButton setImage:[UIImage imageNamed:@"illness_rb_img_nor.png"] forState:UIControlStateNormal];
    
    [self.chooseButton setImage:[UIImage imageNamed:@"illness_rb_img_sel.png"] forState:UIControlStateSelected];
    
    [self.chooseButton addTarget:self action:@selector(clickChooseButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.passwordText.mas_bottom).offset(60);
        make.leading.equalTo(self.view).offset(80);
    }];
    
    /**
     *  阅读文本
     */
    self.readLabel.text = @"本人同意并阅读";
    
    self.readLabel.font = [UIFont systemFontOfSize:14];
    
    self.readLabel.textColor = [UIColor blackColor];
    
    [self.readLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.chooseButton.mas_trailing).offset(10);
        make.centerY.equalTo(self.chooseButton);
    }];
    
    /**
     *  用户协议文本
     */
    self.protocolLabel.text = @"用户协议";
    
    self.protocolLabel.textColor = [UIColor colorWithRed:0.1373 green:0.6549 blue:0.9451 alpha:1.0];
    
    self.protocolLabel.font = [UIFont systemFontOfSize:16];
    
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tanchuang:)];
    [self.protocolLabel addGestureRecognizer:tap];
    
    self.protocolLabel.userInteractionEnabled = YES;
    
    //添加下划线
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"用户协议" attributes:attribtDic];
    self.protocolLabel.attributedText = attribtStr;
    
    [self.protocolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.readLabel.mas_trailing).offset(5);
        make.centerY.equalTo(self.readLabel);
    }];
    
    /**
     *  注册按钮
     */
    
    [self.registerButton setBackgroundImage:[UIImage imageNamed:@"link_button_01"] forState:UIControlStateNormal];
    
    [self.registerButton setTitle:@"注册" forState:UIControlStateNormal];
    
    [self.registerButton setTintColor:[UIColor whiteColor]];
    
    [self.registerButton addTarget:self action:@selector(clickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
    
     [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
         
         make.centerX.equalTo(self.view);
         make.centerY.equalTo(self.view).multipliedBy(1.05);
         make.width.equalTo(@255);
         make.height.equalTo(@38);
    }];
}

- (void)tanchuang:(UITapGestureRecognizer *)tap{
    
    [SVProgressHUD showWithStatus:@"获取协议中"];
    
    //设置黑色遮罩
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    //设置消失时间
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // time-consuming task
        [NSThread sleepForTimeInterval:1];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
    });
}

#pragma mark -- 点击勾选协议按钮

- (void)clickChooseButton{

    /**
     *  按钮的selected为BOOL类型,取反使其默认状态与选中状态循环交替
     */
    
    self.chooseButton.selected = !self.chooseButton.selected;
    NSLog(@"%d",self.chooseButton.selected);
    
}

#pragma mark -- 点击获取验证按钮

- (void)clickTestButton{

    if(!_timer){
    
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeScend) userInfo:nil repeats:YES];
        
        self.timer = timer;
    }
    
}

#pragma mark -- 点击注册账号按钮

- (void)clickRegisterButton{

    [SVProgressHUD showSuccessWithStatus:@"注册成功!"];
    
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [SVProgressHUD dismiss];
        
    });
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 点击获取验证按钮倒计时

-(void)changeScend{

    changedSecond --;
    
    NSString *string = [NSString stringWithFormat:@"%ldS",changedSecond+1];
    
    [self.testButton setTitle:[NSString stringWithFormat:@"%@后重新获取验证码",string] forState:UIControlStateNormal];
    
    if(changedSecond >= -1){
    
        self.testButton.enabled = NO;

    }
    
    else if (changedSecond < -1){
    
        /**
         *   移除定时器,最好与nil一起写
         */
        
        [self.timer invalidate];
        
        self.timer = nil;
        
        changedSecond = 10;
        
        [self.testButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        
        self.testButton.enabled = YES;
    }


}

@end
